const Koa = require("koa");
const Router = require("koa-router");
const views = require("koa-views");
const static = require("koa-static");
const koaBody = require("koa-body");
let app = new Koa();
let router = new Router();
app.use(koaBody());
app.use(views(__dirname+"/views"));
app.use(static(__dirname+"/static"));
router.get("/index",async ctx=>{
   await ctx.render("index.html");
});

router.get("/product",async ctx=>{
    await ctx.render("product.html");
});

router.get("/getdata",ctx=>{
    // 接收get传递过来的参数
    let data = ctx.query;
    ctx.body = data;
})

router.post("/postdata",ctx=>{
    // ctx.body  ---> ctx.response.body
    console.log( ctx.request.body)
    // let str = "";
    // ctx.req.on("data",chunk=>{
    //         str += chunk;
    // })
    // ctx.req.on("end",()=>{
    //     console.log(str);
    // })
    ctx.body = ctx.request.body
})

app.use(router.routes());
app.listen(8989);